package com.eveinfo.display.asset;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.content.Context;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.eveinfo.EveInfo;
import com.eveinfo.R;
import com.eveinfo.api.asset.ApiAsset;
import com.eveinfo.sql.data.Item;
import com.eveinfo.tools.ActivityLauncher;
import com.eveinfo.tools.Tools;

public class CargoAdapter extends ArrayAdapter<ApiAsset> {

	private Context context;
	private GestureDetector gestureDetector;

	private final ArrayList<ApiAsset> assets;

	class Point {
		float x;
		float y;
	}

	private Point point = new Point();

	public CargoAdapter(Context context, int textViewRessourceID, ListView listView, ArrayList<ApiAsset> assets,
			GestureDetector gestureDetector) {
		super(context, textViewRessourceID, assets);
		this.context = context;
		this.assets = assets;
		this.gestureDetector = gestureDetector;
		listView.setAdapter(this);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View view = convertView;
		if (view == null) {
			LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = vi.inflate(R.layout.itemr2, null);
		}

		final ApiAsset asset = assets.get(position);
		final Item type = asset.getItem();

		ImageView pictureIV = (ImageView) view.findViewById(R.id.IVItemR2Picture);
		pictureIV.setImageDrawable(Tools.getItemPicture(type, EveInfo.getPreferences().getAssetsImageSize()));
		TextView nameTV = (TextView) view.findViewById(R.id.TVItemR2Name);
		nameTV.setText(type.getTypeName());
		TextView numberTV = (TextView) view.findViewById(R.id.TVItemR2Number);
		final int quantity = asset.getQuantity();
		numberTV.setText(""+quantity);
		
		view.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (gestureDetector.onTouchEvent(event)){
					return true;
				}
				final int action = event.getAction();
				final float x = event.getRawX();
				final float y = event.getRawY();
				if (action == MotionEvent.ACTION_UP && point.x == x && point.y == y) {
					ActivityLauncher.displayItem(context, type.getTypeID());
					return true;
				} else if (action == MotionEvent.ACTION_DOWN) {
					point.x = x;
					point.y = y;
					return false;
				}
				return false;
			}
		});
		view.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {
				return true;
			}
		});
		return view;
	}

	public void setSort(Comparator<ApiAsset> sorter) {
		Collections.sort(assets, sorter);
	}
}
